<template>
  <div class="app">
    <div class="login iconfont icon-logo"></div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          class="register"
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit(values) {
      //console.log("submit", values);
      //   this.$toast.fail("请输入完整信息");
      this.$axios({
        url: "/login",
        method: "POST",
        data: {
          username: this.username,
          password: this.password,
        },
      })
        .then((res) => {
          console.log(res);
          const { data, statusCode, message } = res.data;
          if (message == "登录成功") {
            this.$toast.success(message);
            // 登录成功后将token和用户id保存到本地
            localStorage.setItem("token", data.token);
            localStorage.setItem("userId", data.user.id);
          } else {
            this.$toast.fail(res.message);
          }
        })
        .catch((err) => {
          this.$toast.success("404");
        });
    },
  },
};
</script>

<style lang='less' scoped>
.app {
  padding: 20/750 * 100vw;
}
.login {
  text-align: center;
  font-size: 160/ 750 * 100vw;
  margin: 100 / 750 * 100vw;
}
.register {
  margin: 50px 0;
}
</style>